﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsAppTest.Dao;

namespace WinFormsAppTest
{
    public partial class OuttimeUpdateForm : Form
    {
        public int outid;
        public DateTime begintime;
        public DateTime endtime;
        public string place;
        public int workid;
        public int userid;
        public string adminname;
        public int adminid;

        public OuttimeUpdateForm()
        {
            InitializeComponent();
        }

        public OuttimeUpdateForm(int outid, DateTime begintime, DateTime endtime,
                                string place, int workid, int userid,
                                string adminname, int adminid)
        {
            this.outid = outid;
            this.begintime = begintime;
            this.endtime = endtime;
            this.place = place;
            this.workid = workid;
            this.userid = userid;
            this.adminname = adminname;
            this.adminid = adminid;
            InitializeComponent();
        }



        //public OuttimeUpdateForm(int uid, string uname, string upassword, string aname, int aid)
        //{
        //    username = uname;
        //    password = upassword;
        //    adminname = aname;
        //    adminid = aid;
        //    userid = uid;
        //    InitializeComponent();
        //}



        private void OuttimeUpdate_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                // 关闭进程
                Process.GetCurrentProcess().Kill();
            }
        }

        private void OuttimeUpdate_Load(object sender, EventArgs e)
        {
            AdminDao adminDao = new AdminDao();
            BegindateTimePicker.Value = begintime;
            EnddateTimePicker.Value = endtime;
            PlacetextBox.Text = place;
            WorkIdlabel.Text = workid.ToString();
            Idlabel.Text = userid.ToString();
            Namelabel.Text = adminDao.GetUserById(userid)[0].Name;

        }

        private void Exitbutton_Click(object sender, EventArgs e)
        {
            this.Hide();
            OuttimeForm f = new OuttimeForm(adminname, adminid);
            f.ShowDialog();
            this.Dispose();
        }

        private void OKbutton_Click(object sender, EventArgs e)
        {
            if (BegindateTimePicker.Value == DateTime.MinValue ||
                BegindateTimePicker.Value == null)
            {
                MessageBox.Show("更新的开始时间不能为空！");
            }
            else if (EnddateTimePicker.Value == DateTime.MinValue ||
                     EnddateTimePicker.Value == null)
            {
                MessageBox.Show("更新的结束时间不能为空！");
            }
            else if (string.IsNullOrWhiteSpace(PlacetextBox.Text))
            {
                MessageBox.Show("出差地点不能为空！");
            }
            else
            {
                //直接调用dao层进行添加
                OuttimeDao Outtimedao = new OuttimeDao();
                Outtimedao.UpdateOuttimeById(outid, BegindateTimePicker.Value,
                                            EnddateTimePicker.Value, PlacetextBox.Text);
                MessageBox.Show("更新成功！");
                OuttimeForm f2 = new OuttimeForm(adminname, adminid);
                this.Hide();
                f2.ShowDialog();
                this.Dispose();

            }
        }

        private void Okbutton_Click(object sender, EventArgs e)
        {

            if (BegindateTimePicker.Value == DateTime.MinValue ||
                BegindateTimePicker.Value == null)
            {
                MessageBox.Show("更新的开始时间不能为空！");
            }
            else if (EnddateTimePicker.Value == DateTime.MinValue ||
                     EnddateTimePicker.Value == null)
            {
                MessageBox.Show("更新的结束时间不能为空！");
            }
            else if (string.IsNullOrWhiteSpace(PlacetextBox.Text))
            {
                MessageBox.Show("出差地点不能为空！");
            }
            else
            {
                //直接调用dao层进行添加
                OuttimeDao Outtimedao = new OuttimeDao();
                Outtimedao.UpdateOuttimeById(outid, BegindateTimePicker.Value,
                                            EnddateTimePicker.Value, PlacetextBox.Text);
                MessageBox.Show("更新成功！");
                OuttimeForm f2 = new OuttimeForm(adminname, adminid);
                this.Hide();
                f2.ShowDialog();
                this.Dispose();

            }
        }
    }
}
